package com.heath_bar.twitter;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	final String URL = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=centralline&include_rts=1&count=1";
	final String APIKEY = "aUb6DSewqlNKvOEBBNA6Zw";
	final String APISECRET = "GkSatmpvYhc9kw5egMejvfjqSFVtrNNv91feDLUzWik";
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		new GetBearerTokenTask().execute();

		Button btn_get_feed = (Button)findViewById(R.id.btn_get_feed);
		btn_get_feed.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				TextView txt_bearer_token = (TextView)findViewById(R.id.txt_bearer_token);
				String bearer_token = txt_bearer_token.getText().toString();
				new GetFeedTask().execute(bearer_token, URL);
			}
		});
	}

	
	
	protected class GetBearerTokenTask extends AsyncTask<Void, Void, String> {
    	
        @Override
		protected String doInBackground(Void... params) {
			
			try {
				DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
				HttpPost httppost = new HttpPost("https://api.twitter.com/oauth2/token");
				
				String apiString = APIKEY + ":" + APISECRET;
				String authorization = "Basic " + Base64.encodeToString(apiString.getBytes(), Base64.NO_WRAP);
		
				httppost.setHeader("Authorization", authorization);
				httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
				httppost.setEntity(new StringEntity("grant_type=client_credentials"));
		
				InputStream inputStream = null;
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
		
				inputStream = entity.getContent();
				BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
				StringBuilder sb = new StringBuilder();
		
				String line = null;
				while ((line = reader.readLine()) != null)
				{
				    sb.append(line + "\n");
				}
				
				return sb.toString();
			}catch (Exception e){
				Log.e("GetBearerTokenTask", "Error:" + e.getMessage());
				return null;
			}
		}
		
		@Override
		protected void onPostExecute(String jsonText){
			try {
				JSONObject root = new JSONObject(jsonText);
				String bearer_token = root.getString("access_token");			
				
				//TextView txt = (TextView)findViewById(R.id.txt_bearer_token);
				//txt.setText(bearer_token);
				
				//TextView txt_bearer_token = (TextView)findViewById(R.id.txt_bearer_token);
				//String bearer_token = txt_bearer_token.getText().toString();
				new GetFeedTask().execute(bearer_token, URL);
				
			}catch (Exception e){
				Log.e("GetBearerTokenTask", "Error:" + e.getMessage());
			}
		}
    }



	
	
	protected class GetFeedTask extends AsyncTask<String, Void, String> {
		
	    @Override
		protected String doInBackground(String... params) {
			
			try {
				DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
				HttpGet httpget = new HttpGet(params[1]);
				httpget.setHeader("Authorization", "Bearer " + params[0]);
				httpget.setHeader("Content-type", "application/json");

				InputStream inputStream = null;
				HttpResponse response = httpclient.execute(httpget);
				HttpEntity entity = response.getEntity();

				inputStream = entity.getContent();
				BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
				StringBuilder sb = new StringBuilder();

				String line = null;
				while ((line = reader.readLine()) != null)
				{
					sb.append(line + "\n");
				}
				return sb.toString();
			}catch (Exception e){
				Log.e("GetFeedTask", "Error:" + e.getMessage());
				return null;
			}
		}
		
		@Override
		protected void onPostExecute(String jsonText){
			try {
				TextView txt = (TextView)findViewById(R.id.txt_feed);
				txt.setText(jsonText);

			}catch (Exception e){
				Log.e("GetFeedTask", "Error:" + e.getMessage());
			}
		}
	}
	
	
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;	
		
	}

}
